package com.heifeng.cloudshelf.utils;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class LogUtils {

    private static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Log/";

    public static void e(String content) {
        if (content.length() > 4000) {
            for (int i = 0; i < content.length(); i += 4000) {
                //当前截取的长度<总长度则继续截取最大的长度来打印
                if (i + 4000 < content.length()) {
                    Log.e("LogUtils" + i, content.substring(i, i + 4000));
                } else {
                    //当前截取的长度已经超过了总长度，则打印出剩下的全部信息
                    Log.e("LogUtils" + i, content.substring(i, content.length()));
                }
            }
        } else {
            //直接打印
            Log.e("LogUtils", content);
        }

        //打印log
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String logPath = path + "LOG-YHJ-" + System.currentTimeMillis() + ".log";
        file = new File(logPath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//        write(logPath, content, true);
    }

    //把信息写入单文件
    public static void write(String path, String content, Boolean isAppend) {
        try {
            File file = new File(path);
            if (!file.exists())
                file.createNewFile();
            FileWriter fileWriter = new FileWriter(file, isAppend);
            fileWriter.write(content);
            fileWriter.close();
            return;
        } catch (IOException ex) {
            ex.printStackTrace();
            return;
        }
    }
}
